var uk = "_uk_", nk = "_nk_", pk = "_pk_", rpk = "_rpk_";
$(function () {
    layui.use("layer");
    $("#username").focus();
    $("#username").val($.localStorage.get(uk));
    $("#nickname").val($.localStorage.get(nk));
    $("#password").val($.localStorage.get(pk));
    $("#repassword").val($.localStorage.get(rpk));
    /** 按了回车键 */
    $(document).keydown(function (event) {
        if (event.keyCode == 13) {
            $("#register-submit-btn").trigger("click");
        }
    })

    /** 给注册按钮绑定点击事件  */
    $("#register-submit-btn").on("click", function () {
        /** 校验注册参数 ctrl+K */
        var nickname = $("#nickname").val();
        var username = $("#username").val();
        var password = $("#password").val();
        var repassword = $("#repassword").val();

        //密码强度
        // var strongRegex = new RegExp("^(?=.{15,20})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\\W).*$");
        // var mediumRegex = new RegExp("^(?=.{10,14})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$",);


        var msg = "";

        if (!/^\w{6,20}$/.test(username)) {
            msg = "账号长度必须是6~20之间";
        } else if (!/^\w{6,20}$/.test(password) || !/^\w{6,20}$/.test(repassword)) {
            msg = "密码长度必须是6~20之间";
        } else if ((password) != (repassword)) {
            msg = "两次密码输入不一致";
        }
        //else if ((password) = (repassword)) {
        //     msg = "两次密码输入一致";
        // }
        // else if (/^\w{16,20}$/.test(password)) {
        //     msg = "密码强度：强！";
        // } else if (/^\w{9,15}$/.test(password)) {
        //     msg = "密码强度：适中~";
        // } else if (/^\w{6,8}$/.test(password)){
        //     msg = "密码强度：弱！";
        // }

        //信息框
        if (msg != "") {
            $("#tishi").html(msg);
            return;
        }

        $.ajax({
            type: "POST",
            url: "/api/user/register",
            async: true,
            data: {
                nickname: nickname.trim(),
                username: username.trim(),
                password: password.trim()
            },
            dataType: "json",
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.MsgBox.Alert("消息", "出错了，请于管理员联系");
            },
            success: function (json) {
                if (json.code == 200) {
                    sessionStorage['userInfo'] = JSON.stringify(json.data.userInfo);
                    $.MsgBox.Confirm("消息", "注册并登录成功！",);
                    setTimeout(function () {
                        window.location.replace("main.html");
                    }, 2000);
                    //$.MsgBox.Alert("消息","注册成功，返回登录页登录！");
                    //window.location.replace("loginForm.html");
                } else if (json.msg) {
                    $("#tishi").html(json.msg);
                } else {
                    $.MsgBox.Alert("消息", "出错了，请于管理员联系");
                }
            }
        });
    })
})
// //如果当前窗口不是最上层窗口，那么就把自己变为最上层窗口。
// if(window !=top){
// 	top.location.href=location.href;
// }
